package servlet;

import com.alibaba.fastjson.JSON;
import pojo.Ticket;
import pojo.User;
import service.TicketService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * 查询用户的所有购票记录
 * 输入一个用户对象user
 * 输出一个List<Ticket>
 */
@WebServlet("/allMyTicketsServlet")
public class AllMyTicketsServlet extends HttpServlet {
    private final TicketService service = new TicketService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = request.getReader().readLine();
        User user = JSON.parseObject(jsonString, User.class);

        List<Ticket> tickets = service.selectAllTicketsOfUserNo(user);

        jsonString = JSON.toJSONString(tickets);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
